<script setup lang="ts">
import AdditionalFormDateTime from './component/AdditionalFormDateTime.vue';
import AdditionalFormInput from './component/AdditionalFormInput.vue';
import AdditionalFormLocation from './component/AdditionalFormLocation.vue';
import AdditionalFormMedia from './component/AdditionalFormMedia.vue';
import AdditionalFormPicker from './component/AdditionalFormPicker.vue';
import AdditionalFormRecording from './component/AdditionalFormRecording.vue';
import AdditionalFormRadio from './component/AdditionalFormRadio.vue';

interface IProps {
  formList: IGoodsAdditionalInfo[];
}

type EmitEvents = {
  (event: 'change', payload: { data: IGoodsAdditionalInfo }): void;
};

const emits = defineEmits<EmitEvents>();
const props = defineProps<IProps>();
const formList = ref<IGoodsAdditionalInfo[]>(props.formList);

// 处理身份证id查询到的患者信息
const handleUserDataByIDCard = (data: IObtainUserDataByIDCardRes) => {
  formList.value = formList.value.map((item) => {
    if (Object.keys(data).some((key) => item.fieldName === key)) {
      return { ...item, value: data[item.fieldName] ? data[item.fieldName].toString() : '' };
    }
    return item;
  });
};
</script>

<template>
  <view v-for="item in formList" :key="item.attrId">
    <AdditionalFormInput v-if="item.fieldType === 1" :data="item" @change="({ value }) => emits('change', { data: { ...item, value } })" @IDCardChange="handleUserDataByIDCard" />
    <AdditionalFormPicker v-if="item.fieldType === 2" :data="item" @change="({ value }) => emits('change', { data: { ...item, value } })" />
    <AdditionalFormDateTime v-if="item.fieldType === 3" :data="item" @change="({ value }) => emits('change', { data: { ...item, value } })" />
    <AdditionalFormMedia v-if="item.fieldType === 4" :data="item" type="image" @change="({ value }) => emits('change', { data: { ...item, value } })" />
    <AdditionalFormMedia v-if="item.fieldType === 5" :data="item" type="video" @change="({ value }) => emits('change', { data: { ...item, value } })" />
    <AdditionalFormRadio v-if="item.fieldType === 6" :data="item" @change="({ value }) => emits('change', { data: { ...item, value } })" />
    <AdditionalFormLocation v-if="item.fieldType === 7" :data="item" @change="({ value }) => emits('change', { data: { ...item, value } })" />
    <AdditionalFormRecording v-if="item.fieldType === 8" :data="item" @change="({ value }) => emits('change', { data: { ...item, value } })" />
  </view>
</template>
